package com.ibigcat.module.mdm.controller.admin.commoditysetgroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.commoditysetgroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CommoditySetGroupDO;
import com.ibigcat.module.mdm.service.CommoditySetGroupService;

@Tag(name = "管理后台 - 组套商品管理明细")
@RestController
@RequestMapping("/mdm/commodity-set-group")
@Validated
public class CommoditySetGroupController {

    @Resource
    private CommoditySetGroupService commoditySetGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建组套商品管理明细")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:create')")
    public CommonResult<Long> createCommoditySetGroup(@Valid @RequestBody CommoditySetGroupSaveReqVO createReqVO) {
        return success(commoditySetGroupService.createCommoditySetGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组套商品管理明细")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:update')")
    public CommonResult<Boolean> updateCommoditySetGroup(@Valid @RequestBody CommoditySetGroupSaveReqVO updateReqVO) {
        commoditySetGroupService.updateCommoditySetGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组套商品管理明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:delete')")
    public CommonResult<Boolean> deleteCommoditySetGroup(@RequestParam("id") Long id) {
        commoditySetGroupService.deleteCommoditySetGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组套商品管理明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:query')")
    public CommonResult<CommoditySetGroupRespVO> getCommoditySetGroup(@RequestParam("id") Long id) {
        CommoditySetGroupDO commoditySetGroup = commoditySetGroupService.getCommoditySetGroup(id);
        return success(BeanUtils.toBean(commoditySetGroup, CommoditySetGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组套商品管理明细分页")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:query')")
    public CommonResult<PageResult<CommoditySetGroupRespVO>> getCommoditySetGroupPage(@Valid CommoditySetGroupPageReqVO pageReqVO) {
        PageResult<CommoditySetGroupDO> pageResult = commoditySetGroupService.getCommoditySetGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommoditySetGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组套商品管理明细 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:commodity-set-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommoditySetGroupExcel(@Valid CommoditySetGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommoditySetGroupDO> list = commoditySetGroupService.getCommoditySetGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组套商品管理明细.xls", "数据", CommoditySetGroupRespVO.class,
                        BeanUtils.toBean(list, CommoditySetGroupRespVO.class));
    }

}